package com.summer.peony.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.summer.peony.system.mapper.SysMenuMapper;
import com.summer.peony.system.pojo.SysMenu;
import com.summer.peony.system.pojo.SysMenuExample;
import com.summer.peony.system.pojo.SysMenuExample.Criteria;
import com.summer.peony.system.pojo.SysUser;
import com.summer.peony.system.service.SysMenuService;

@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;

	@Override
	public List<SysMenu> selectMenusByUser(SysUser sysUser) {
		// TODO 根据用户查菜单
		List<SysMenu> menus = new LinkedList<SysMenu>();
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		// 管理员显示所有菜单信息
		if (sysUser.isAdmin()) {
			criteria.andDelFlagEqualTo("0");
			menus = sysMenuMapper.selectByExample(example);
		} else {
			criteria.andDelFlagEqualTo("0").andParentIdEqualTo(sysUser.getUserId());
		}
		return getChildPerms(menus, 0);
	}

	@Override
	public Integer addMenu(SysMenu sysMenu) {
		// TODO 新增菜单
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andPermissionEqualTo(sysMenu.getPermission());
		List<SysMenu> list = sysMenuMapper.selectByExample(example);
		if (list.size() > 0 && !sysMenu.getPermission().isEmpty()) {
			return 2;
		}
		Date date = new Date();
		sysMenu.setCreateTime(date);
		sysMenu.setUpdateTime(date);
		sysMenu.setDelFlag("0");
		return sysMenuMapper.insert(sysMenu);
	}

	@Override
	public Integer upMenu(SysMenu sysMenu) {
		// TODO 修改菜单
		SysMenu sysMenu2 = sysMenuMapper.selectByPrimaryKey(sysMenu.getMenuId());
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andPermissionEqualTo(sysMenu.getPermission()).andMenuIdNotEqualTo(sysMenu.getMenuId())
				.andDelFlagEqualTo("0");
		List<SysMenu> list = sysMenuMapper.selectByExample(example);
		if (list.size() > 0 && !sysMenu.getPermission().isEmpty()) {
			return 2;
		}
		Date date = new Date();
		sysMenu.setUpdateTime(date);
		sysMenu.setCreateTime(sysMenu2.getCreateTime());
		sysMenu.setDelFlag(sysMenu2.getDelFlag());
		return sysMenuMapper.updateByPrimaryKey(sysMenu);
	}

	@Override
	public Integer deleteByPrimaryKey(Integer menuId) {
		// TODO 删除菜单
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0").andParentIdEqualTo(menuId);
		List<SysMenu> list = sysMenuMapper.selectByExample(example);
		if (list.isEmpty()) {
			SysMenu sysMenu = sysMenuMapper.selectByPrimaryKey(menuId);
			sysMenu.setDelFlag("1");
			return sysMenuMapper.updateByPrimaryKey(sysMenu);
		}
		return 2;
	}

	@Override
	public List<SysMenu> menuList() {
		// TODO 菜单列表
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0");
		List<SysMenu> list = sysMenuMapper.selectByExample(example);
		return getChildPerms(list, 0);
	}

	@Override
	public SysMenu byIdMenu(Integer menuId) {
		// TODO 查询菜单详情
		return sysMenuMapper.selectByPrimaryKey(menuId);
	}

	/**
	 * 根据父节点的ID获取所有子节点
	 * 
	 * @param list     分类表
	 * @param parentId 传入的父节点ID
	 * @return String
	 */
	public List<SysMenu> getChildPerms(List<SysMenu> list, int parentId) {
		List<SysMenu> returnList = new ArrayList<SysMenu>();
		for (Iterator<SysMenu> iterator = list.iterator(); iterator.hasNext();) {
			SysMenu t = (SysMenu) iterator.next();
			// 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
			if (t.getParentId() == parentId) {
				recursionFn(list, t);
				returnList.add(t);
			}
		}
		return returnList;
	}

	/**
	 * 递归列表
	 * 
	 * @param list
	 * @param t
	 */
	private void recursionFn(List<SysMenu> list, SysMenu t) {
		// 得到子节点列表
		List<SysMenu> childList = getChildList(list, t);
		t.setChildren(childList);
		for (SysMenu tChild : childList) {
			if (hasChild(list, tChild)) {
				// 判断是否有子节点
				Iterator<SysMenu> it = childList.iterator();
				while (it.hasNext()) {
					SysMenu n = (SysMenu) it.next();
					recursionFn(list, n);
				}
			}
		}
	}

	/**
	 * 得到子节点列表
	 */
	private List<SysMenu> getChildList(List<SysMenu> list, SysMenu t) {
		List<SysMenu> tlist = new ArrayList<SysMenu>();
		Iterator<SysMenu> it = list.iterator();
		while (it.hasNext()) {
			SysMenu n = (SysMenu) it.next();
			if (n.getParentId().longValue() == t.getMenuId().longValue()) {
				tlist.add(n);
			}
		}
		return tlist;
	}

	/**
	 * 判断是否有子节点
	 */
	private boolean hasChild(List<SysMenu> list, SysMenu t) {
		return getChildList(list, t).size() > 0 ? true : false;
	}

	@Override
	public List<SysMenu> menuListAll() {
		// 查询菜单列表
		SysMenuExample example = new SysMenuExample();
		Criteria criteria = example.createCriteria();
		criteria.andDelFlagEqualTo("0");
		return sysMenuMapper.selectByExample(example);
	}

}
